package com.wy.component.communication;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.commons.lang.StringUtils;

import com.wy.framework.constants.Constants;
import com.wy.component.properties.PropertiesUtil;



public class SMSsender  extends Thread   {
        public static String strURL =  "http://211.99.191.148";
        public static  String nameSpace = "http://info.pica.com";
        private String SHCNN_URI_SEND = "/mms/services/info";
        private static SMSsender api = null;
        private String mobileNo;
        private String msg;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String date=sdf.format(new Date());
	    
	     public SMSsender(String  mobileNo,String msg) {
	    	this.mobileNo = mobileNo;
	    	if(StringUtils.isBlank(msg)){
	    		msg="";
	    	}
	    	this.msg=msg;
	     }

	     public void run() {
	    	 try {
				if(!StringUtils.equals(new PropertiesUtil().getAppProperties("projectName"), Constants.PROJECT_NAME_DH))
					System.out.println(	"sendSMS =>"+ sendEx( "YCWL-CRM-0100-OWOIBY", "16378387",msg,mobileNo,"000"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}	      				  
	     }
    
    private String sendEx(String Regcode, String Pwd,String content,String mobile,String exnum){
        String result = "";
        byte [] b;
        boolean isSentOk=false;
        int sendtimes=0;
        while(!isSentOk){
	        try { 
	            Service services = new Service();
                Call call1 = null;
                call1 = (Call) services.createCall();
                call1.setTargetEndpointAddress(new URL(strURL+SHCNN_URI_SEND));
                call1.setOperationName(new QName(nameSpace,"sendSMS"));
                call1.addParameter(new QName(nameSpace,"in0"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in1"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in2"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in3"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in4"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in5"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in6"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in7"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in8"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.addParameter(new QName(nameSpace,"in9"), XMLType.XSD_STRING, ParameterMode.IN);
                call1.setReturnType(XMLType.XSD_STRING);
                call1.setUseSOAPAction(true);
                call1.setSOAPActionURI("http://zzwx/example");
                result = (String) call1.invoke(new Object[] {Regcode, Pwd, mobile, java.net.URLEncoder.encode(content,"gbk"),"", "1", "", "1", "", "4"}); 
	            b = result.getBytes("8859_1");
	            String name = new String(b, "GBK");
	            if(name.equals("0")){  
	            	isSentOk=true;
	            }else{	
	            	sendtimes ++;
	            	if (sendtimes>1){
	            		isSentOk=true;
	            	}else
	            	{
	            	 System.out.println("发送结果：" + name);
		        	 isSentOk=false;
	            	}
	            }
	        }catch(Exception ex){
	        	System.out.println("ex----");
	        	isSentOk=true;
	        }
        }
        if (isSentOk) {
        	if (sendtimes>1)
        		return "-2";
        	else
        	return "0";
        }else {
            return "-1";
        }
    }
 
    
    public static void main(String[] args) {
    	
//    	 for(int i = 0; i <3; i++) 
    		 new SMSsender("13816041373","测试咯【签名】").start();
    }
}